
let CURRENT_CAFE = 'currentCafe'
let CURRENT_SERVER = 'currentServer'
let clearItem = key => {
  sessionStorage.removeItem(key)
}

let setItem = (key, obj) => {
  sessionStorage.setItem(key, JSON.stringify(obj))
}

let getItem = (key) => {
  let obj = sessionStorage.getItem(key)
  if (obj) {
    return JSON.parse(obj)
  } else {
    return null
  }
}

export default {
  state: {
    showMenu: true,
    // 屏幕默认宽度
    windowWidth: 1366,
    cafeGroupTimeStamp: Date.now(),
    title: '',
    loadingMenu: false,
    // 机房列表
    machineRoomList: [],
    menuList: [],
    currentMenu: null,
    currentCafe: getItem(CURRENT_CAFE),
    currentServer: getItem(CURRENT_SERVER),
    tableContextMenu: {
      show: false
    },
    showUpdatePasswordModal: false
  },
  mutations: {
    toggleMenu (state) {
      state.showMenu = !state.showMenu
    },
    setWindowWidth (state, width) {
      state.windowWidth = width
    },
    changeCafeGroupTimeStamp (state) {
      state.cafeGroupTimeStamp = Date.now()
    },
    clearAppCache (state) {
      state.machineRoomList = []
      state.menuList = []
      state.currentMenu = null
      state.currentCafe = null
      state.currentServer = null

      clearItem(CURRENT_CAFE)
      clearItem(CURRENT_SERVER)
    },
    setTitle (state, title) {
      state.title = title
    },
    setLoadingMenu (state, loadingMenu) {
      state.loadingMenu = loadingMenu
    },
    setMachineRoomList (state, machineRoomList = []) {
      state.machineRoomList = machineRoomList
    },
    setMenuList (state, menuList = []) {
      state.menuList = menuList
    },
    setCurrentMenu (state, menu) {
      state.currentMenu = menu
    },
    setCurrentCafe (state, cafe) {
      // 由于刷新页面时，当前网吧无法获取，需要缓存
      state.currentCafe = cafe
      setItem(CURRENT_CAFE, cafe)
    },
    setCurrentServer (state, server) {
      state.currentServer = server
      setItem(CURRENT_SERVER, server)
    },
    // table 右键点击参数
    setTableContextMenu (state, obj) {
      state.tableContextMenu = obj
    },
    changeUpdatePasswordModal (state, show = true) {
      state.showUpdatePasswordModal = show
    }
  },
  actions: {
    changeTitle ({ commit }, title) {
      commit('setTitle', title)
    },
    changeLoadingMenu ({ commit }, loading) {
      commit('setLoadingMenu', loading)
    },
    changeMachineRoomList ({ commit }, machineRoomList) {
      commit('setMachineRoomList', machineRoomList)
    },
    changeMenuList ({ commit }, menuList) {
      commit('setMenuList', menuList)
    },
    changeMenuItem ({ commit }, menu) {
      commit('setCurrentMenu', menu)
    },
    changeCafe ({ commit }, cafe) {
      commit('setCurrentCafe', cafe)
    },
    changeServer ({ commit }, server) {
      commit('setCurrentServer', server)
    },
    changeTableContextMenu ({ commit }, menu) {
      commit('setTableContextMenu', menu)
    },
    changeUpdatePasswordModal ({ commit }, show) {
      commit('changeUpdatePasswordModal', show)
    }
  }
}
